package com.ldq.imooc.basics.dataOperation;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @Author : 李道泉
 * @Description :
 * @Date : 2018/4/28 9:53
 * @Modified by :
 */
public class GoddessView {

    private static final String CONTEXT = "欢迎来到女神禁区：\n" +
            "下面是女神禁区的功能列表：\n" +
            "[MAIN/M]:主菜单\n" +
            "[QUERY/Q]:查看全部女神的信息\n" +
            "[GET/G]:查看某位女神的详细信息\n" +
            "[ADD/A]:添加女神信息\n" +
            "[UPDATE/U]:更新女神信息\n" +
            "[DELETE/D]:删除女神信息\n" +
            "[SEARCH/S]:查询女神信息（根据姓名、手机号查询）\n" +
            "[EXIT/E]:退出女神禁区\n" +
            "[BREAK/B]:返回主菜单";

    private static final String OPERATION_MAIN = "MAIN";
    private static final String OPERATION_QUERY = "QUERY";
    private static final String OPERATION_GET = "GET";
    private static final String OPERATION_ADD = "ADD";
    private static final String OPERATION_UPDATE = "UPDATE";
    private static final String OPERATION_DELETE = "DELETE";
    private static final String OPERATION_SEARCH = "SEARCH";
    private static final String OPERATION_BREAK = "BREAK";
    private static final String OPERATION_EXIT = "EXIT";

    public static void main(String[] args) {

        System.out.println(CONTEXT);

        Scanner scan = new Scanner(System.in);
        Goddess goddess = new Goddess();
        GoddessAction action = new GoddessAction();
        String previous = null;
        Integer step = 1;

        while (scan.hasNext()) {

            String in = scan.next().toString();
            if (OPERATION_MAIN.equals(in.toUpperCase()) || OPERATION_MAIN.substring(0, 1).equals(in.toUpperCase()) || OPERATION_MAIN.equals(previous)) {
                previous = OPERATION_MAIN;
                System.out.println(CONTEXT);

            } else if (OPERATION_QUERY.equals(in.toUpperCase()) || OPERATION_QUERY.substring(0, 1).equals(in.toUpperCase())) {

                try {
                    List<Goddess> list = action.query();
                    for (Goddess go : list) {
                        System.out.println(go.getId() + ",姓名 ：" + go.getUser_name());
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            } else if (OPERATION_GET.equals(in.toUpperCase()) || OPERATION_GET.substring(0, 1).equals(in.toUpperCase()) || OPERATION_GET.equals(previous)) {

                previous = OPERATION_GET;
                if (1 == step) {
                    System.out.println("请输入需要获取女神信息的【ID】");
                    try {
                        List<Goddess> list = action.query();
                        for (Goddess go : list) {
                            System.out.println(go.getId() + ",姓名 ：" + go.getUser_name());
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (2 == step) {
                    try {
                        goddess = action.get(Integer.valueOf(in));
                        System.out.println(goddess.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (OPERATION_GET.equals(previous)) {
                    step++;
                    if (step > 3) {
                        previous = null;
                        step = 1;
                    }
                }

            } else if (OPERATION_ADD.equals(in.toUpperCase()) || OPERATION_ADD.substring(0, 1).equals(in.toUpperCase()) || OPERATION_ADD.equals(previous)) {
                previous = OPERATION_ADD;
                if (1 == step) {
                    System.out.println("请输入女神的【姓名】");
                } else if (2 == step) {
                    goddess.setUser_name(in);
                    System.out.println("请输入女神的【年龄】");
                } else if (3 == step) {
                    goddess.setAge(Integer.valueOf(in));
                    System.out.println("请输入女神的【生日】，格式 ：yyyy-MM-dd");
                } else if (4 == step) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date birthday = null;
                    try {
                        birthday = sdf.parse(in);
                        goddess.setBirthday(birthday);
                        System.out.println("请输入女神的【邮箱】");
                    } catch (ParseException e) {
                        e.printStackTrace();
                        System.out.println("输入生日格式有误，请重新输入");
                        step = 3;
                    }

                } else if (5 == step) {
                    goddess.setEmail(in);
                    System.out.println("请输入女神的【手机号】");
                } else if (6 == step) {
                    goddess.setMobile(in);

                    try {
                        action.add(goddess);
                        System.out.println("添加女神信息成功");
                    } catch (SQLException e) {
                        e.printStackTrace();
                        System.out.println("添加女神信息失败");
                    }
                }
                if (OPERATION_ADD.equals(previous)) {
                    step++;
                    if (step > 6) {
                        previous = null;
                        step = 1;
                    }
                }

            } else if (OPERATION_BREAK.equals(in.toUpperCase()) || OPERATION_BREAK.substring(0, 1).equals(in.toUpperCase()) || OPERATION_BREAK.equals(previous)) {

                previous = OPERATION_MAIN;

            } else if (OPERATION_EXIT.equals(in.toUpperCase()) || OPERATION_EXIT.substring(0, 1).equals(in.toUpperCase())) {

                System.out.println("成功退出女神禁区。");
                break;
            } else {
                System.out.println("您输入的值为：" + in);
            }
        }
    }
}
